package com.road2trip.gwt.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.maps.client.InfoWindow;
import com.google.gwt.maps.client.InfoWindowContent;
import com.google.gwt.maps.client.MapWidget;
import com.google.gwt.maps.client.control.LargeMapControl;
import com.google.gwt.maps.client.geocode.Geocoder;
import com.google.gwt.maps.client.geocode.LatLngCallback;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.client.ui.RootPanel;

/** 
 * Entry point for the place page 
 */
public class EPPlace implements EntryPoint {

	private MapWidget map;

	@Override
	public void onModuleLoad() {
		
		final String placeName = RootPanel.get("placeName").getElement().getInnerHTML(); 
		GWT.log(placeName, null);
		
		LatLng cawkerCity = LatLng.newInstance(0,0);// Open a map centered on 0,0
		map = new MapWidget(cawkerCity, 6);
		map.setSize("500px", "300px");				
		map.addControl(new LargeMapControl());// Add some controls for the zoom level

		final InfoWindow info = map.getInfoWindow();	
		Geocoder geocoder = new Geocoder();
		geocoder.getLatLng(placeName, new LatLngCallback() {
			public void onFailure() {
				//@TODO
			}
			
			public void onSuccess(LatLng point) {											
				map.setCenter(point, 6);
				Marker marker = new Marker(point);
				map.addOverlay(marker);
				info.open(marker, new InfoWindowContent(placeName));
			}
		});

		// Add the map to the HTML host page
		RootPanel.get("map").add(map);		
	}

}
